package com.moody.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.MemberInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * MemberInfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.MemberInfo
 * @author MyEclipse Persistence Tools
 */

public class MemberInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(MemberInfoDAO.class);
	// property constants
	public static final String CUST_ID = "custId";
	public static final String GROUP_NAME = "groupName";
	public static final String ORG_ID = "orgId";
	public static final String CURRENCY = "currency";
	public static final String CREDIT_BALANCE = "creditBalance";
	public static final String BEGIN_DATE = "beginDate";
	public static final String END_DATE = "endDate";
	public static final String CREDIT_REMAINING = "creditRemaining";
	public static final String MEMBER_SHIP = "memberShip";

	protected void initDao() {
		// do nothing
	}

	public void save(MemberInfo transientInstance) {
		log.debug("saving MemberInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MemberInfo persistentInstance) {
		log.debug("deleting MemberInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MemberInfo findById(java.lang.String id) {
		log.debug("getting MemberInfo instance with id: " + id);
		try {
			MemberInfo instance = (MemberInfo) getHibernateTemplate().get(
					"com.moody.hibernate.domain.MemberInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MemberInfo instance) {
		log.debug("finding MemberInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MemberInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MemberInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCustId(Object custId) {
		return findByProperty(CUST_ID, custId);
	}

	public List findByGroupName(Object groupName) {
		return findByProperty(GROUP_NAME, groupName);
	}

	public List findByOrgId(Object orgId) {
		return findByProperty(ORG_ID, orgId);
	}

	public List findByCurrency(Object currency) {
		return findByProperty(CURRENCY, currency);
	}

	public List findByCreditBalance(Object creditBalance) {
		return findByProperty(CREDIT_BALANCE, creditBalance);
	}

	public List findByBeginDate(Object beginDate) {
		return findByProperty(BEGIN_DATE, beginDate);
	}

	public List findByEndDate(Object endDate) {
		return findByProperty(END_DATE, endDate);
	}

	public List findByCreditRemaining(Object creditRemaining) {
		return findByProperty(CREDIT_REMAINING, creditRemaining);
	}

	public List findByMemberShip(Object memberShip) {
		return findByProperty(MEMBER_SHIP, memberShip);
	}

	public List findAll() {
		log.debug("finding all MemberInfo instances");
		try {
			String queryString = "from MemberInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MemberInfo merge(MemberInfo detachedInstance) {
		log.debug("merging MemberInfo instance");
		try {
			MemberInfo result = (MemberInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MemberInfo instance) {
		log.debug("attaching dirty MemberInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MemberInfo instance) {
		log.debug("attaching clean MemberInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static MemberInfoDAO getFromApplicationContext(ApplicationContext ctx) {
		return (MemberInfoDAO) ctx.getBean("MemberInfoDAO");
	}

	public void update(MemberInfo memberInfo) {
		log.debug("saving MemberInfo instance");
		try {
			getHibernateTemplate().update(memberInfo);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
}